// 根据权限生成菜单
export default {
    namespaced: true,  // 必须添加这行来启用命名空间
    state: {
        navBar: {
            active: '0',
            list: []
        },
    },
    getters: {
        // 当用户登录后没有权限进入bendIndex，设置登录的首页
        // 首页
        adminIndex(state) {
            if (!state.navBar || !state.navBar.list || state.navBar.list.length === 0) {
                return 'BackendIndex';
            }
            const item = state.navBar.list[state.navBar.active];
            if (item && item.submenu && item.submenu.length > 0) {
                return item.submenu[0].pathname;
            }
            return 'BackendIndex';
        },
    },
    mutations: {
        /*
 msg": "ok",
    "data": {
        "id": 3,
        "status": 1,
        "create_time": "2019-08-05 00:57:47",
        "update_time": "2020-02-05 00:55:05",
        "username": "admin",
        "avatar": "http:\/\/tangzhe123-com.oss-cn-shenzhen.aliyuncs.com\/public\/5e39862c67d41.jpg",
        "role_id": 2,
        "super": 1,
        "role": {
            "id": 2,
            "status": 1,
            "create_time": "2019-08-11 15:34:39",
            "update_time": "2019-08-11 20:42:30",
            "name": "超级管理员",
            "desc": "最高权限",
            "rules": [
                {
                    "id": 5,
                    "rule_id": 0,
                    "status": 1,
                    "create_time": "2019-08-11 13:36:09",
                    "update_time": "2020-02-07 16:46:37",
                    "name": "首页",
                    "desc": "index",
                    "frontpath": null,
                    "condition": null,
                    "menu": 1,
                    "order": 21,
                    "icon": null,
                    "method": "GET",
                    "pivot": {
                        "id": 163,
                        "role_id": 2,
                        "rule_id": 5
                    }
                },
                {
                    "id": 6,
                    "rule_id": 0,
                    "status": 1,
                    "create_time": "2019-08-11 13:36:36",
                    "update_time": "2019-08-11 13:36:36",
                    "name": "商品",
                    "desc": "shop_goods_list",
                    "frontpath": null,
                    "condition": null,
                    "menu": 1,
                    "order": 20,
                    "icon": null,
                    "method": "GET",
                    "pivot": {
                        "id": 165,
                        "role_id": 2,
                        "rule_id": 6
                    }
                },
 
        */
        //  创建菜单
        createNavBar(state, menus) {
            // console.log('收到的菜单数据：', menus);
            
            // 检查menus是否为数组
            if (!Array.isArray(menus)) {
                console.error('菜单数据格式错误：', menus);
                state.navBar = {
                    active: '0',
                    list: []
                };
                return;
            }

            // 处理菜单数据
            state.navBar = {
                active: '0',
                list: menus.map((item) => {
                    // 确保submenu是数组
                    const submenu = Array.isArray(item.child) ? item.child.map(subItem => {
                        // 检查并处理pathname
                        let pathname = subItem.pathname;
                        if (!pathname && subItem.desc) {
                            // 如果pathname不存在但有desc，使用desc作为pathname
                            pathname = subItem.desc.replace(/_/g, '/');
                        }
                        
                        return {
                            icon: subItem.icon || 'el-icon-menu',
                            name: subItem.name,
                            pathname: pathname
                        };
                    }) : [];

                    return {
                        name: item.name,
                        subActive: '0',
                        submenu
                    };
                })
            };

            // 保存到sessionStorage
            sessionStorage.setItem('navBar', JSON.stringify(state.navBar));
            // console.log('处理后的菜单数据：', state.navBar);
        },

        //初始化菜单
        initNavBar(state) {
            const navBar = window.sessionStorage.getItem('navBar');
            if (navBar) {
                try {
                    state.navBar = JSON.parse(navBar);
                } catch (e) {
                    console.error('解析菜单数据失败：', e);
                    state.navBar = {
                        active: '0',
                        list: []
                    };
                }
            } else {
                state.navBar = {
                    active: '0',
                    list: []
                };
            }
        },
    }
}
